import 'package:distribution_mall_flutter/constant/cf_constant.dart';
import 'package:distribution_mall_flutter/http/modules/address.dart';
import 'package:distribution_mall_flutter/provider/home/waterfall.dart';
import 'package:distribution_mall_flutter/utils/common_widget.dart';
import 'package:distribution_mall_flutter/utils/utils.dart';
import 'package:distribution_mall_flutter/widgets/icon_button.dart';
import 'package:easy_debounce/easy_throttle.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:getwidget/getwidget.dart';

class EPModalButton extends ConsumerStatefulWidget {
  final List<Widget> children;
  final String? title;
  final String? subTitle;
  final String? buttonText;
  final EdgeInsets? footerMargin;
  final EdgeInsets? titleMargin;
  final BoxBorder? titleBorder;
  final double maxHeightScale;
  final Widget otherTitle;
  final Widget? customScroll;
  final Widget footerChild;
  final bool hasDefaultFooter;
  final Widget? child;
  final EdgeInsets? padding;
  final BoxDecoration decoration;
  final Function()? onFooterPressed;
  final Future Function()? loadMore;
  final CrossAxisAlignment? headerCrossAxis;
  final bool isLoading;
  final EdgeInsets headerPadding;
  final Future Function(Map<String, dynamic> params)? onRefresh;

  const EPModalButton(
      {super.key,
      required this.children,
      this.child,
      this.title,
      this.subTitle,
      this.buttonText,
      this.titleMargin = const EdgeInsets.only(bottom: EdgeConstant.edge15),
      this.titleBorder = const Border(
          bottom: BorderSide(color: ColorConstant.greyOp151, width: 0.9)),
      this.footerMargin,
      this.padding,
      this.maxHeightScale = 0.85,
      this.otherTitle = const SizedBox.shrink(),
      this.customScroll,
      this.footerChild = const SizedBox.shrink(),
      this.hasDefaultFooter = false,
      this.onFooterPressed,
      this.loadMore,
      this.headerCrossAxis,
      this.onRefresh,
      this.isLoading = false,
      this.headerPadding =
          const EdgeInsets.only(right: 21.8, bottom: EdgeConstant.edge5),
      this.decoration = const BoxDecoration(color: Colors.white)});

  @override
  ConsumerState createState() => _EPModalButton();
}

class _EPModalButton extends ConsumerState<EPModalButton> {
  final ScrollController _scrollController =
      ScrollController(initialScrollOffset: 0);
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      if (widget.loadMore != null && widget.loadMore is Function()) {
        int gap = Utils.getDeviceInfo()['gap'];
        _scrollController.addListener(() async {
          EasyThrottle.throttle(
              'modal_button_scroll', const Duration(milliseconds: 500), () {
            var px = _scrollController.position.pixels;
            if (px > _scrollController.position.maxScrollExtent - gap) {
              widget.loadMore!();
            }
          });
        });
      }
    });
  }

  Widget titleRender({required BuildContext context}) {
    return Column(
      children: [
        Container(
          margin: widget.titleMargin,
          padding: widget.headerPadding,
          constraints: const BoxConstraints(maxHeight: 50),
          decoration: BoxDecoration(border: widget.titleBorder),
          child: Row(
              crossAxisAlignment:
                  widget.headerCrossAxis ?? CrossAxisAlignment.center,
              children: [
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.only(left: 24),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(widget.title ?? '',
                            textAlign: TextAlign.center,
                            style: const TextStyle(
                                fontSize: 16.4,
                                color: Colors.black,
                                // fontWeight: FontWeight.bold,
                                letterSpacing: TextConstant.letterSpacing,
                                decoration: TextDecoration.none)),
                        Visibility(
                            visible: widget.subTitle != null,
                            child: Padding(
                              padding: const EdgeInsets.only(top: 0),
                              child: Text(widget.subTitle ?? '',
                                  style: const TextStyle(
                                      fontSize: 10.9,
                                      color: Color.fromRGBO(134, 141, 150, 1),
                                      letterSpacing: TextConstant.letterSpacing,
                                      decoration: TextDecoration.none)),
                            ))
                      ],
                    ),
                  ),
                ),
                EPIconButton(
                  width: 35,
                  height: 35,
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                  icon: const Icon(Icons.close_outlined,
                      size: 26, color: Color.fromRGBO(3, 0, 0, 1)),
                )
              ]),
        ),
        widget.otherTitle
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTextStyle(
        style: const TextStyle(letterSpacing: TextConstant.letterSpacing),
        child: Padding(
          padding:
              EdgeInsets.only(bottom: MediaQuery.of(context).padding.bottom),
          child: Container(
              // height: MediaQuery.of(context).size.height * maxHeightScale,
              padding: widget.padding ??
                  const EdgeInsets.symmetric(
                      vertical: EdgeConstant.edge15,
                      horizontal: EdgeConstant.edge10),
              constraints: BoxConstraints(
                maxHeight:
                    MediaQuery.of(context).size.height * widget.maxHeightScale,
              ),
              decoration: widget.decoration,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  titleRender(context: context),
                  Expanded(
                      child: widget.customScroll ??
                          SingleChildScrollView(
                            controller: _scrollController,
                            child: (widget.child != null)
                                ? widget.child
                                : Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: widget.children,
                                  ),
                          )),
                  widget.hasDefaultFooter
                      ? Container(
                          width: double.infinity,
                          // margin: widget.footerMargin ??
                          //     const EdgeInsets.symmetric(
                          //         horizontal: EdgeConstant.edge218),
                          padding: const EdgeInsets.fromLTRB(
                              EdgeConstant.edge218,
                              EdgeConstant.edge146,
                              EdgeConstant.edge218,
                              8.2),
                          decoration: const BoxDecoration(
                            border: Border(
                                top: BorderSide(
                                    width: 0.9,
                                    color: ColorConstant.grey233_4)),
                            // borderRadius:
                            //     BorderRadius.all(Radius.circular(7.3)
                            //     )
                          ),
                          child: CommonBtnWidget.generalBtn(
                            widget.onFooterPressed,
                            shape: GFButtonShape.square,
                            height: 38.2,
                            fullWidthButton: true,
                            isLoading: widget.isLoading,
                            text: widget.buttonText,
                          ))
                      : widget.footerChild
                ],
              )),
        ));
  }
}
